package com.app.activitys;




import com.example.applucianokikao.R;
import com.facebook.FacebookAuthorizationException;
import com.facebook.FacebookOperationCanceledException;
import com.facebook.LoginActivity;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphObject;
import com.facebook.model.GraphUser;
import com.facebook.model.OpenGraphAction;
import com.facebook.model.OpenGraphObject;
import com.facebook.widget.FacebookDialog;
import com.facebook.widget.FacebookDialog.OpenGraphActionDialogBuilder;
import com.facebook.widget.LoginButton;
import com.facebook.widget.FacebookDialog.PendingCall;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainFacebook extends FragmentActivity{

	
//	private MainFragment mainFragment;
	
	private UiLifecycleHelper uiHelper;
	private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
        	System.out.println("Entrou no call");
            onSessionStateChange(session, state, exception);
        }
    };
    private FacebookDialog.Callback dialogCallback = new FacebookDialog.Callback() {
        @Override
        public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
            Log.d("HelloFacebook", String.format("Error: %s", error.toString()));
        }

        @Override
        public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
        	System.out.println("Entrou no dialogCallBack");
            Log.d("HelloFacebook", "Success!");
        }
    };


    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		

		uiHelper = new UiLifecycleHelper(this, callback);
		uiHelper.onCreate(savedInstanceState);
	
		setContentView(R.layout.testeface);
		
		LoginButton authButton = (LoginButton) findViewById(R.id.authButton);
		/*
		if (savedInstanceState == null) {
        	// Add the fragment on initial activity setup
        	mainFragment = new MainFragment();
            getSupportFragmentManager()
            .beginTransaction()
            .add(android.R.id.content, mainFragment)
            .commit();
        } else {
        	// Or set the fragment from restored state info
        	mainFragment = (MainFragment) getSupportFragmentManager()
        	.findFragmentById(android.R.id.content);
        }
		*/
	}
	
    
	private boolean hasPublishPermission() {
	   Session session = Session.getActiveSession();
	   return session != null && session.getPermissions().contains("publish_actions");
	}
	
	private void onSessionStateChange(Session session, SessionState state, Exception exception) {
	    if (state.isOpened()) {
	        Log.i("Estado", "Logged in...");
	        if(hasPublishPermission()){
	     
	        	
	        	OpenGraphObject dish = OpenGraphObject.Factory.createForPost("lucianokikao:dish");
	        
	        	dish.setTitle("Comida");
	        	//composicao.getData().setProperty("title_o", "Testando");
				//composicao.setProperty("url", "http://www.google.com.br/imgres?imgurl=&imgrefurl=http%3A%2F%2Fdivirta-se.correioweb.com.br%2Fmaterias.htm%3Fmateria%3D7812%26secao%3DGuia%26data%3D20090717&h=0&w=0&sz=1&tbnid=B9x3eGW5ks-G0M&tbnh=183&tbnw=276&zoom=1&docid=TCZCX79_adHZ1M&ei=-xEEU9TnBoaqkQes-4HgDQ&ved=0CAUQsCUoAQ");
				dish.setProperty("image", "http://www.lucianokikao.com.br/imagens/52db074df34d51390085965.jpg");
				dish.setProperty("description", "Comida ao som de musicas");
				
				OpenGraphAction action = GraphObject.Factory.create(OpenGraphAction.class);
				action.setType("lucianokikao:eat");
				action.setProperty("dish", dish);
				//action.setExplicitlyShared(true);
				action.setProperty("fb:explicitly_shared", "true");
				
				FacebookDialog shareDialog = new FacebookDialog.OpenGraphActionDialogBuilder(this, action, "dish").build();																						
				//FacebookDialog shareDialog = new FacebookDialog.OpenGraphActionDialogBuilder(this, action, "dish").build();
				
				//FacebookDialog shareDialog = new FacebookDialog.OpenGraphActionDialogBuilder(this, action, "composicao").build();
				
				uiHelper.trackPendingDialogCall(shareDialog.present());
				
	        }else{
	        	session.requestNewPublishPermissions(new Session.NewPermissionsRequest(this, "publish_actions"));
	        }
			
	    } else if (state.isClosed()) {
	        Log.i("Estado", "Logged out...");
	    }
	    
	}

	@Override
	public void onResume() {
	    super.onResume();
	    System.out.println("onResume");
	    uiHelper.onResume();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	    super.onActivityResult(requestCode, resultCode, data);
	    System.out.println("onResult");
	    uiHelper.onActivityResult(requestCode, resultCode, data, dialogCallback);
	}
	@Override
	public void onPause() {
	    super.onPause();
	    uiHelper.onPause();
	}

	@Override
	public void onDestroy() {
	    super.onDestroy();
	    uiHelper.onDestroy();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
	    super.onSaveInstanceState(outState);
	    uiHelper.onSaveInstanceState(outState);
	}

}
